package com.jwell.panrank.presenter

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jwell.driverapp.tools.BaseSubscriber
import com.jwell.driverapp.ui.base.BasePresenter
import com.jwell.panrank.contract.PreRegisterPlaneContract
import com.jwell.panrank.entity.InlineBean
import com.jwell.panrank.model.PreRegisterPlaneModel
import org.json.JSONObject
import org.reactivestreams.Subscription

/**
 * Created by 76577 on 2018/4/11.
 * 预登记验收计划
 */
class PreRegisterPlanePresenter : PreRegisterPlaneContract.Presenter, BasePresenter<PreRegisterPlaneContract.View>() {


    private val model: PreRegisterPlaneModel by lazy {
        PreRegisterPlaneModel()
    }

    override fun createFuelPlan(requestData: PreRegisterPlaneModel.RequestData) {
        model.createFuelPlan(requestData, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                getView()?.createSucceed()
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.createFailed(msg)
            }
        })
    }

    override fun getContractListByProName(id: String) {
        model.getContractListByProName(id, object : BaseSubscriber() {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                var data = Gson().fromJson<List<InlineBean>>(data.getString("result"), object : TypeToken<List<InlineBean>>() {
                }.type)
                getView()?.showInlineData(data)
            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                getView()?.showInlineData(null)
            }
        })
    }
}